package com.example.license.controller;

import com.example.license.entity.LicenseCreatorParam;
import com.example.license.service.LicenseCreateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author YangJian
 * @Description 创建证书控制层
 * @createTime 2022年03月23日 10:00
 */
@RestController
@RequestMapping("/api/license")
public class LicenseCreateController {
    @Autowired
    private LicenseCreateService service;

    /**
     * @Description 创建证书
     * @Author YJ
     * @Date  2022/3/28 17:53
     * @Param [param]
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    @PostMapping("/create")
    public Map<String,Object> licenseCreate(@RequestBody  LicenseCreatorParam param){
        Map<String,Object> resultMap = new HashMap<>(2);
        //生成证书
        try {
            service.licenseCreate(param);
            resultMap.put("result","success");
            resultMap.put("message","服务器端生成证书成功!");
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result","error");
            resultMap.put("message","服务器端生成证书失败!,请检查参数是否正确");
        }
        return resultMap;
    }

    /**
     * @Description 证书下载
     * @Author YJ
     * @Date  2022/3/28 17:54
     * @Param [response]
     * @return javax.servlet.http.HttpServletResponse
     **/
    @GetMapping("/download")
    public HttpServletResponse download(HttpServletResponse response){
        return service.download(response);
    }
}
